<!DOCTYPE html>
<html>
<head>
    <!-- 页面meta -->
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title>传智健康</title>
    <meta name="description" content="传智健康">
    <meta name="keywords" content="传智健康">
    <meta content="width=device-width,initial-scale=1,maximum-scale=1,user-scalable=no" name="viewport">
    <!-- 引入样式 -->
    <link rel="stylesheet" href="../plugins/elementui/index.css">
    <link rel="stylesheet" href="../plugins/font-awesome/css/font-awesome.min.css">
    <link rel="stylesheet" href="../css/style.css">
</head>
<body class="hold-transition">
<div id="app">
    <div class="content-header">
        <h1>用户管理</h1>
        <el-breadcrumb separator-class="el-icon-arrow-right" class="breadcrumb">
            <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
            <el-breadcrumb-item>系统设置</el-breadcrumb-item>
            <el-breadcrumb-item>用户管理</el-breadcrumb-item>
        </el-breadcrumb>
    </div>
    <div class="app-container">
        <div class="box">
            <div class="filter-container">
                <el-input placeholder="用户名称" v-model="pagination.queryString" style="width: 200px;" class="filter-item"></el-input>
                <el-button @click="findPage()" class="dalfBut">查询</el-button>
                <el-button type="primary" class="butT" @click="handleCreate()">新建</el-button>
            </div>
            <el-table size="small" current-row-key="id" :data="dataList" stripe highlight-current-row>
                <el-table-column type="index" align="center" label="序号"></el-table-column>
                <el-table-column prop="username" label="用户名称" align="center"></el-table-column>
                <el-table-column label="性别" align="center">
                    <template slot-scope="scope">
                        <span>{{ scope.row.gender == '1' ? '男' : '女'}}</span>
                    </template>
                </el-table-column>
                <el-table-column  label="生日" align="center">
                    <template slot-scope="scope">
                        <span>{{scope.row.birthday &&scope.row.birthday.substring(0,11)}}</span>
                    </template>
                </el-table-column>
                <el-table-column prop="telephone" label="用户电话" align="center"></el-table-column>
                <el-table-column label="状态" align="center">
                    <template slot-scope="scope">
                        <span>{{ scope.row.station == '1' ? '正常' : '被冻结'}}</span>
                    </template>
                </el-table-column>
                <el-table-column prop="remark" label="备注" align="center" width="250"></el-table-column>
                <el-table-column label="操作" align="center" width="400">
                    <template slot-scope="scope">
                        <el-button type="primary" size="mini" @click="handleUpdate(scope.row)">编辑</el-button>
                        <el-button type="primary" size="mini" @click="handleControlStation(scope.row)">账号冻结/解冻</el-button>
                        <el-button type="danger" size="mini" @click="handleResetPassword(scope.row)">重置密码</el-button>
                        <el-button size="mini" type="danger" @click="handleDelete(scope.row)">删除</el-button>
                    </template>
                </el-table-column>
            </el-table>
            <div class="pagination-container">
                <el-pagination
                        class="pagiantion"
                        @current-change="handleCurrentChange"
                        :current-page="pagination.currentPage"
                        :page-size="pagination.pageSize"
                        layout="total, prev, pager, next, jumper"
                        :total="pagination.total">
                </el-pagination>
            </div>
            <!-- 新增标签弹层 -->
            <div class="add-form">
                <el-dialog title="新增用户" :visible.sync="dialogFormVisible">
                    <el-form ref="dataAddForm" :model="formData" :rules="rules" label-position="right" label-width="100px">
                        <el-row>
                            <el-col :span="15">
                                <el-form-item label="用户名称" prop="username" >
                                    <el-input v-model="formData.username" @blur="UserNameCode()"/>
                                </el-form-item>
                            </el-col>
                        </el-row>
                        <el-row>
                            <el-col :span="15">
                                <el-form-item label="性别" >
                                    <el-radio v-model="formData.gender" label="1">男</el-radio>
                                    <el-radio v-model="formData.gender" label="0">女</el-radio>
                                </el-form-item>
                            </el-col>
                        </el-row>
                        <el-row>
                            <el-col :span="15">
                                <el-form-item label="出生日期">
                                    <el-date-picker
                                            v-model="formData.birthday"
                                            type="date"
                                            placeholder="选择日期"
                                            format="yyyy 年 MM 月 dd 日"
                                            value-format="yyyy-MM-dd">
                                    </el-date-picker>
                                </el-form-item>
                            </el-col>
                        </el-row>
                        <el-row>
                            <el-col :span="15">
                                <el-form-item label="拥有角色">
                                    <el-select v-model="roleIds" multiple placeholder="请选择角色">
                                        <el-option
                                                v-for="item in roleTableData"
                                                :key="item.id"
                                                :label="item.name"
                                                :value="item.id">
                                        </el-option>
                                    </el-select>
                                </el-form-item>
                            </el-col>
                        </el-row>
                        <el-row>
                            <el-col :span="15">
                                <el-form-item label="用户电话" prop="telephone">
                                    <el-input v-model="formData.telephone" type="text"></el-input>
                                </el-form-item>
                            </el-col>
                        </el-row>
                        <el-row>
                            <el-col :span="15">
                                <el-form-item label="备注">
                                    <el-input  v-model="formData.remark"/>
                                </el-form-item>
                            </el-col>
                        </el-row>
                    </el-form>
                    <div slot="footer" class="dialog-footer">
                        <el-button @click="dialogFormVisible = false">取消</el-button>
                        <el-button type="primary" @click="handleAdd()">确定</el-button>
                    </div>
                </el-dialog>
            </div>

            <!-- 编辑标签弹层 -->
            <div class="add-form">
                <el-dialog title="编辑用户" :visible.sync="dialogFormVisible4Edit">
                    <el-form ref="dataAddForm" :model="formData" :rules="rules" label-position="right" label-width="100px">
                        <el-row>
                            <el-col :span="15">
                                <el-form-item label="性别" >
                                    <el-radio v-model="formData.gender" label="1">男</el-radio>
                                    <el-radio v-model="formData.gender" label="0">女</el-radio>
                                </el-form-item>
                            </el-col>
                        </el-row>
                        <el-row>
                            <el-col :span="15">
                                <el-form-item label="出生日期">
                                    <el-date-picker
                                            v-model="formData.birthday"
                                            type="date"
                                            placeholder="选择日期"
                                            format="yyyy 年 MM 月 dd 日"
                                            value-format="yyyy-MM-dd">
                                    </el-date-picker>
                                </el-form-item>
                            </el-col>
                        </el-row>
                        <el-row>
                            <el-col :span="15">
                                <el-form-item label="拥有角色">
                                    <el-select v-model="roleIds" multiple placeholder="请选择角色">
                                        <el-option
                                                v-for="item in roleTableData"
                                                :key="item.id"
                                                :label="item.name"
                                                :value="item.id">
                                        </el-option>
                                    </el-select>
                                </el-form-item>
                            </el-col>
                        </el-row>
                        <el-row>
                            <el-col :span="15">
                                <el-form-item label="用户电话">
                                    <el-input v-model="formData.telephone" type="text"></el-input>
                                </el-form-item>
                            </el-col>
                        </el-row>
                        <el-row>
                            <el-col :span="15">
                                <el-form-item label="备注">
                                    <el-input v-model="formData.remark"/>
                                </el-form-item>
                            </el-col>
                        </el-row>
                    </el-form>
                    <div slot="footer" class="dialog-footer">
                        <el-button @click="dialogFormVisible4Edit = false">取消</el-button>
                        <el-button type="primary" @click="handleEdit()">确定</el-button>
                    </div>
                </el-dialog>
            </div>
        </div>
    </div>
</div>
</body>
<!-- 引入组件库 -->
<script src="../js/vue.js"></script>
<script src="../plugins/elementui/index.js"></script>
<script type="text/javascript" src="../js/jquery.min.js"></script>
<script src="../js/axios-0.18.0.js"></script>
<script>
    var vue = new Vue({
        el: '#app',
        data:{
            pagination: {//分页相关模型数据
                currentPage: 1,//当前页码
                pageSize:10,//每页显示的记录数
                total:0,//总记录数
                queryString:null//查询条件
            },
            dataList: [],//当前页要展示的分页列表数据
            roleIds:[], //新增和编辑表单中菜单项对应的复选框，基于双向绑定可以进行回显和数据提交
            //角色列表
            roleTableData:[{
                roleId: '选项1',  //传到后台的角色id
                roleName: '健康管理师'  //显示前台的角色名称
            },
                {
                    roleId: '选项2',  //传到后台的角色id
                    roleName: '健康xxxx'  //显示前台的角色名称
                },
            ],
            formData: {},//表单数据
            dialogFormVisible: false,//增加表单是否可见
            dialogFormVisible4Edit:false,//编辑表单是否可见
            rules: {//校验规则
                username: [{ required: true, message: '请填写用户名', trigger: 'blur' }],
                telephone: [{ required: true, message: '请填写密码', trigger: 'blur' }]
            }
        },
        //钩子函数，VUE对象初始化完成后自动执行
        created() {
            this.findPage();
        },
        methods: {
            //权限不足提示
            showMessage(r){
                if(r == 'Error: Request failed with status code 403'){
                    //权限不足
                    this.$message.error('您暂时还没有该权限');
                    return;
                }else{
                    this.$message.error('未知错误');
                    return;
                }
            },
            UserNameCode(){
                axios.get("/user/findByName.do?userName="+this.formData.username)
                    .then(response=>{
                        if (response.data.flag) {

                        }else{
                            this.$message.error(response.data.message);
                            //如果改用户存在,清空表单数据
                            this.formData={};
                        }
                    })
            },
            //编辑
            handleEdit() {
                //1.提交表单前验证
                this.$refs["dataAddForm"].validate((valid) => {
                    if (valid) {
                        //2.关闭弹出
                        this.dialogFormVisible4Edit = false;
                        //3..发送新增检查项请求
                        axios.post('../user/edit.do?roleIds='+this.roleIds,this.formData).then(res=>{
                            //成功回显数据
                            if(res.data.flag){
                                //提示成功
                                this.$message({
                                    message: res.data.message,
                                    type: 'success'
                                });
                                //跳转重新查询
                                this.findPage();
                            }else {
                                //失败错误提示
                                this.$message.error(res.data.message);
                            }
                        }).catch(error => {
                            this.showMessage(error);
                        });
                    }
                });
            },
            //添加
            handleAdd () {
                //1.提交表单前验证
                this.$refs["dataAddForm"].validate((valid) => {
                    if (valid) {
                        //2.关闭弹出
                        this.dialogFormVisible = false;
                        //3..发送新增检查项请求
                        axios.post('../user/add.do?roleIds='+this.roleIds,this.formData).then(res=>{
                            //成功回显数据
                            if(res.data.flag){
                                //提示成功
                                this.$message({
                                    message: res.data.message,
                                    type: 'success'
                                });
                                //跳转重新查询
                                this.findPage();
                            }else {
                                //失败错误提示
                                this.$message.error(res.data.message);
                            }
                        }).catch(error => {
                            this.showMessage(error);
                            console.log(error);
                        });
                    }
                });
            },
            //分页查询
            findPage() {
                var queryPageBean =
                    {
                        currentPage:this.pagination.currentPage, //当前页码
                        pageSize:this.pagination.pageSize, //当前页显示条数
                        queryString:this.pagination.queryString//查询条件
                    };
                ///分页查询
                axios.post('/user/findPage.do',queryPageBean)
                    .then(res=>{
                        if(res.data.flag){
                            this.pagination.total = res.data.data.total; //响应总记录数
                            this.dataList = res.data.data.rows; //响应的数据
                        }else{
                            this.$message.warning(res.data.message)
                        }
                    }).catch(error =>{
                    this.showMessage(error);
                    console.log(error);
                })
            },
            // 重置表单
            resetForm() {
                this.formData={};
                this.roleIds=[];
            },
            // 弹出添加窗口
            handleCreate() {
                this.resetForm();
                //获得角色列表
                axios.get("../roleManage/findAll.do")
                    .then(response => {
                        if (response.data.flag) {
                            this.roleTableData = response.data.data;
                            //弹出编辑窗口
                            this.dialogFormVisible=true;
                        } else {
                            this.$message.error(response.data.message);
                        }
                    }).catch(error => {
                    this.showMessage(error);
                    console.log(error);
                });
            },
            // 弹出编辑窗口
            handleUpdate(row) {
                //根据id查询表单数据
                axios.get("../user/findById.do?id="+row.id)
                    .then(response=>{
                        if (response.data.flag){
                            this.formData=response.data.data;
                            //获得角色列表
                            axios.get("../roleManage/findAll.do")
                                .then(response => {
                                    if (response.data.flag) {
                                        this.roleTableData = response.data.data;
                                        axios.get("../user/findRoleIds.do?userId=" + row.id)
                                            .then(response =>{
                                                if (response.data.flag){
                                                    this.roleIds = response.data.data;
                                                    //弹出编辑窗口
                                                    this.dialogFormVisible4Edit=true;
                                                } else {
                                                    this.$message.error(response.data.message);
                                                }
                                            });
                                    } else {
                                        this.$message.error(response.data.message);
                                    }
                                });
                        } else {
                            this.$message.error(response.data.message);
                        }
                    }).catch(error => {
                    this.showMessage(error);
                    console.log(error);
                });
            },
            //切换页码
            handleCurrentChange(currentPage) {
                //重新设置页码
                this.pagination.currentPage = currentPage;
                //重新查询就可以得到数据
                this.findPage();
            },
            // 删除
            handleDelete(row) {
                this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                    axios.get("/user/delete.do?id="+row.id)
                        .then(response=>{
                            if (response.data.flag){
                                this.$message.success(response.data.message);
                                this.findPage();
                            }else {
                                this.$message.error(response.data.message);
                            }
                        }).catch(error => {
                        this.showMessage(error);
                        console.log(error);
                    })
                }).catch(() => {
                    this.$message({
                        type: 'info',
                        message: '已取消删除'
                    });
                });
            },
            //改变用户状态
            handleControlStation(row) {
                this.$confirm('此操作将' + (row.station == 1 ? '冻结':"解冻") +'该用户是否继续?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                    axios.get("../user/changeStationByUsername.do?username=" + row.username + "&station=" + row.station)
                        .then(response => {
                            if (response.data.flag){
                                this.$message.success(response.data.message);
                                this.findPage();
                            }else {
                                this.$message.error(response.data.message);
                            }
                        }).catch(error => {
                        this.showMessage(error);
                        console.log(error);
                    });
                }).catch(() => {
                    this.$message({
                        type: 'info',
                        message: '已取消更改'
                    });
                });

            },
            //重置密码
            handleResetPassword(row) {
                this.$confirm('此操作将重置该用户密码是否继续?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                    axios.get("../user/changePassword.do?username=" + row.username)
                        .then(response => {
                            if (response.data.flag){
                                this.$message.success(response.data.message);
                                this.findPage();
                            }else {
                                this.$message.error(response.data.message);
                            }
                        }).catch(error => {
                        this.showMessage(error);
                        console.log(error);
                    });
                }).catch(() => {
                    this.$message({
                        type: 'info',
                        message: '已取消更改'
                    });
                });
            }
        }
    });
</script>
</html>